package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Employee;
import cn.xpg.service.EmployeeService;
import cn.xpg.tools.PageSupport;

@Controller
public class EmployeeController {

	@Resource(name = "employeeService")
	private EmployeeService employeeService;

	// 分页查询员工信息
	@RequestMapping("/getEmployeeList.do")
	public String getEmployeeList(HttpServletRequest request) {
		String employee_name = request.getParameter("employee_name");
		String jname = request.getParameter("jname");
		String pageIndex = request.getParameter("pageIndex");
		int pageSize = 3;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = employeeService.getEmployeeCount(employee_name);
		System.out.println("查询员工信息！！！！！！！！！！————>getEmployeeList");

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Employee> employeeList = null;
		employeeList = employeeService.getEmployeeList(employee_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("employeeList", employeeList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("employee_name", employee_name);
		if (jname.equals("base")) {
			return "/jsp/base/employee_list";
		} else {
			return "/jsp/system/manage_yg";
		}
	}

	// 新增员工
	@RequestMapping("addEmployee.do")
	public String addEmployee(@Valid Employee employee) {
		System.out.println("新增员工信息！！！！！！！！！！————>addEmployee");
		if (employeeService.addEmployee(employee) > 0) {
			return "redirect:/getEmployeeList.do?jname=base";
		} else {
			return "/jsp/base/employee_list";
		}
	}

	// 获取员工Id
	@RequestMapping("getEmployeeId.do")
	public String getEmployeeId(HttpServletRequest request) {
		System.out.println("获取员工Id！！！！！！！！！！————>getEmployeeId");
		int employee_id = Integer.parseInt(request.getParameter("employee_id"));
		Employee employee = employeeService.getEmployeeId(employee_id);
		request.setAttribute("employee", employee);
		return "/jsp/base/employee_update";
	}

	// 修改员工
	@RequestMapping("updateEmployee.do")
	public String updateEmployee(@Valid Employee employee, HttpServletRequest request) {
		System.out.println("修改员工信息！！！！！！！！！！————>updateEmployee");
		if (employeeService.updateEmployee(employee) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getEmployeeList.do?jname=base";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("employee", employee);
			return "/jsp/base/employee_update";
		}
	}

	// 删除员工
	@RequestMapping("deleteEmployee.do")
	public String deleteEmployee(HttpServletRequest request) {
		System.out.println("删除员工信息！！！！！！！！！！————>deleteEmployee");
		int employee_id = Integer.valueOf(request.getParameter("employee_id"));
		if (employeeService.deleteEmployee(employee_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getEmployeeList.do?jname=base";
	}
}
